package com.blackcat.recode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackcat.recode.common.result.AjaxResult;
import com.blackcat.recode.dao.DeployRecodeMapper;
import com.blackcat.recode.dao.UpdateRecodeMapper;
import com.blackcat.recode.entity.Branch;
import com.blackcat.recode.dao.BranchMapper;
import com.blackcat.recode.entity.DeployRecode;
import com.blackcat.recode.entity.ServiceInfo;
import com.blackcat.recode.entity.UpdateRecode;
import com.blackcat.recode.service.BranchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blackcat.recode.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p> 分支信息表 服务实现类
 * @author blackcat 2024-09-10
 */
@Service
public class BranchServiceImpl extends ServiceImpl<BranchMapper, Branch> implements BranchService {

    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private UpdateRecodeMapper updateRecodeMapper;
    @Autowired
    private DeployRecodeMapper deployRecodeMapper;

    @Override
    public AjaxResult remove(String id) {
        branchMapper.deleteById(id);
        QueryWrapper<UpdateRecode> updateRecodeQueryWrapper = new QueryWrapper<>();
        updateRecodeQueryWrapper.eq("branch_id", id);
        updateRecodeMapper.delete(updateRecodeQueryWrapper);
        QueryWrapper<DeployRecode> deployRecodeQueryWrapper = new QueryWrapper<>();
        deployRecodeQueryWrapper.inSql("update_id", "select id from update_recode where branch_id='"+id+"'");
        deployRecodeMapper.delete(deployRecodeQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getBranchNameList( String serviceId, String projectId) {
        QueryWrapper<Branch> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(serviceId)) {
            queryWrapper.eq("service_id", serviceId);
        }
        if (StringUtils.isNotBlank(projectId)) {
            queryWrapper.eq("project_id", projectId);
        }
        queryWrapper.select("id", "branch_name");
        queryWrapper.orderByAsc("branch_name");
        return AjaxResult.success(branchMapper.selectList(queryWrapper));
    }

    @Override
    public AjaxResult getList(Integer pageNow, Integer pageSize, String branchName, String serviceId, String projectId) {
        QueryWrapper<Branch> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(branchName)) {
            queryWrapper.like("branch_name", branchName);
        }
        if (StringUtils.isNotBlank(serviceId)) {
            queryWrapper.eq("service_id", serviceId);
        }
        if (StringUtils.isNotBlank(projectId)) {
            queryWrapper.eq("project_id", projectId);
        }
        queryWrapper.orderByAsc("branch_name");
        Page<Branch> page = new Page<>(pageNow, pageSize);
        Page<Branch> result = branchMapper.selectPage(page, queryWrapper);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult edit(Branch branch) {
        if (branch.getId() == null) {
            branchMapper.insert(branch);
        }else {
            branchMapper.updateById(branch);
        }
        return AjaxResult.success();
    }
}
